/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_RTSP_PUBLISHER_H
#define MK_RTSP_PUBLISHER_H
#include "MkRtspClient.h"
#include "MkMedia/MkMediaUtil.h"


typedef MkFunction<void(Uint32 ErrorCode)>  MkRtspPusherReadyCallback;
class CMkMediaSource;
class MkApi CMkRtspPublisher : public CMkRtspClient
{
public:
    CMkRtspPublisher(CMkEvent* pEvent, CMkTimer* pTimer, MkVector<CMkRtpUdpServer*>& VecRtpUdpServer, const MkEventRemovedCallback& RemoveCb);
    ~CMkRtspPublisher();

    Uint32 Start(const MkString& RtspUrl, BOOL bTcpStream, const CMkVideoParam& VideoParam, const CMkAudioParam& AudioParam, const MkRtspPusherReadyCallback& ReadyCb);
    Uint32 Stop();
    Uint32 SendEsPacket(const CMkMediaSource* pSource, const MkEsPacket& EsPacket);
private:
    Uint32 OnResponseError(const MkRtspStatusCode& StatusCode) override;
    Uint32 OnOptionResponse(const CMkRtspResponse& Res) override;
    Uint32 OnAnnounceResponse(const CMkRtspResponse& Res) override;
    Uint32 OnSetupResponse(const CMkRtspRequest&  RtspReq, const CMkRtspResponse& Res) override;
    Uint32 OnRecordResponse(const CMkRtspResponse& Res) override;
private:
    Uint32 SendRtp(CMkRtspSubSession* pSubSession, CMkRtpPacket& RtpPacket, const MkEsPacket& EsPacket);
    Uint32 SendH264Rtp(CMkRtspSubSession* pSubSession, CMkRtpPacket& RtpPacket, const MkEsPacket& EsPacket);
    Uint32 SendH265Rtp(CMkRtspSubSession* pSubSession, CMkRtpPacket& RtpPacket, const MkEsPacket& EsPacket);
    Uint32 SendAacRtp(CMkRtspSubSession* pSubSession, CMkRtpPacket& RtpPacket, const MkEsPacket& EsPacket);
    Uint32 SendRtpPacket(CMkRtspSubSession* pSubSession, const CMkRtpPacket& RtpPacket);
private:
    MkString                    m_StrSdp;
    CMkVideoParam               m_VideoParam;
    CMkAudioParam               m_AudioParam;
    MkRtspPusherReadyCallback   m_ReadyCb;
    Uint8                       *m_pPacketBuffer;
    Uint32                      m_nPacketMaxLen;
    Uint32                      m_nPacketOffset;
    BOOL                        m_bReady;
    BOOL                        m_bSendCache;
};




#endif